package com.bjh.controller;

import com.bjh.service.BusinessService;
import com.bjh.util.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/agent/business")
public class BusinessController extends BaseController {
    @Autowired
    private BusinessService businessService;

    /**
     * 获取所属分组
     * @param agentId 代理商id
     * @param search 搜索关键字
     * @param pageIndex 起始页
     * @param pageSize 分页大小
     * @return {@link Map}
     */
    @GetMapping("/getGroupByAgentId")
    public Map<String, Object> getGroupByAgentId (@RequestParam Long agentId, @RequestParam(required = false) String search,
    @RequestParam Long pageIndex, @RequestParam Long pageSize) {
        logBefore(log, "++++++++++ 获取所属分组 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            params.put("search", search);
            params.put("start", (pageIndex - 1) * pageSize);
            params.put("length", pageSize);
            Map<String, Object> resMap = new HashMap<>(businessService.getGroupByAgentId(params));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 获取分组详情
     * @param id 分组id
     * @return {@link Map}
     */
    @GetMapping("/getGroupById")
    public Map<String, Object> getGroupById (@RequestParam Long id) {
        logBefore(log, "++++++++++ 获取分组详情 ++++++++++");
        try{
            Map<String, Object> resMap = new HashMap<>(businessService.getGroupById(id));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 编辑分组
     * @param id 分组id
     * @param agentId 代理商id
     * @param name 代理商名称
     * @return {@link Map}
     */
    @PostMapping("/editGroup")
    public Map<String, Object> editGroup (@RequestParam(required = false) Long id, @RequestParam String agentId, @RequestParam Long saleId,
    @RequestParam String name){
        logBefore(log, "++++++++++ 编辑分组 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            if(Objects.nonNull(id)) params.put("id", id);
            params.put("agentId", agentId);
            params.put("name", name);
            params.put("saleId", String.valueOf(saleId));
            businessService.editGroup(params);
            logAfter(log);
            return returnSuccess();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "编辑失败");
        }
    }

    /**
     * 根据分组id获取商户数量
     * @param id 分组id
     * @return {@link Map}
     */
    @PostMapping("/getBusinessCountById")
    public Map<String, Object> getBusinessCountById (@RequestParam Long id){
        logBefore(log, "++++++++++ 根据分组id获取商户数量 ++++++++++");
        try{
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("count", businessService.getBusinessCountById(id));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "编辑失败");
        }
    }
    /**
     * 删除分组
     * @param id 分组id
     * @return {@link Map}
     */
    @PostMapping("/deleteGroup")
    public Map<String, Object> deleteGroup (@RequestParam Long id){
        logBefore(log, "++++++++++ 删除分组 ++++++++++");
        try{
            businessService.deleteGroup(id);
            logAfter(log);
            return returnSuccess();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "删除失败");
        }
    }

    /**
     * 根据代理商id获取商户列表
     * @param agentId 代理商id
     * @param search 搜素关键字
     * @param groupId 分组id
     * @param pageIndex 起始页
     * @param pageSize 分页大小
     * @return {@link Map}
     */
    @GetMapping("/getBusinessByAgentId")
    public Map<String, Object> getBusinessByAgentId (@RequestParam Long agentId,@RequestParam(required = false) String search,
    @RequestParam(required = false) Long groupId, @RequestParam Long pageIndex, @RequestParam Long pageSize){
        logBefore(log, "++++++++++ 根据代理商id获取商户列表 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            params.put("search", search);
            params.put("groupId", groupId);
            params.put("start", (pageIndex - 1) * pageSize);
            params.put("length", pageSize);
            Map<String, Object> resMap = new HashMap<>(businessService.getBusinessByAgentId(params));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 获取分组选项
     * @param agentId 代理商id
     * @return {@link Map}
     */
    @GetMapping("/getGroupOption")
    public Map<String, Object> getGroupOption (@RequestParam Long agentId){
        logBefore(log, "++++++++++ 获取分组选项 ++++++++++");
        try{
            List<?> resList = new ArrayList<>(businessService.getGroupOption(agentId));
            logAfter(log);
            return returnSuccessWithData(resList);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }
}
